function dataPrep_impurityBC_multi_filling()
%impurity B
% ind = 521:721;
% gate_range = -1.68:0.002:-1.28;

%impurity C
ind = 275:475;
gate_range = -1.68:0.002:-1.28;
%v=0.5 @ -1.474
% full filling = 0.555V

n = linspace(0.129,0.844,length(ind))-1;

pathname = 'raw data/';


for j=1:length(ind)
    [raw_data, ~] = load_3ds(['Grid Spectroscopy' num2str(ind(j),'%.3d') '.3ds'], pathname,nan);
    
    for i=1:length(raw_data)
        if strcmp(raw_data(i).channel,'LockinXchannel')
            dIdV1 = raw_data(i).map;
            bias = raw_data(i).e;
            rx_tmp = raw_data(i).rx.*1e9*0.9;
        end
        if strcmp(raw_data(i).channel,'Lockin2Xchannel') || strcmp(raw_data(i).channel,'Input6')
            dIdV2 = raw_data(i).map;
        end
        if strcmp(raw_data(i).channel,'Z')
            zzz(j,:) = raw_data(i).map;
        end        
    end
    dIdV = dIdV2;
    d2Id2V = dIdV1;
    [nr,~,ne] = size(dIdV);
    rx(j,:) = rx_tmp(1:end);
    for ir = 1:length(rx(j,:))
        %%%% gaussfilt then pick local maximum %%%%
        data_fit(:) = dIdV(ir,1,:);
        data_fit = imgaussfilt(data_fit,4);
        [~ , tmp] = max( data_fit );
%         [~ , tmp] = max( imboxfilt(dIdV(ir,1,:),3) );
        phi_0(j,ir) = bias(tmp);
        
        %using 1Harm to fit
        fit_from = max(1, tmp-6);
        fit_to = min(ne,tmp+6);
        clear tmp2;
        tmp2(:) = data_fit(fit_from:fit_to);
        f = fit(bias(fit_from:fit_to)',tmp2','gauss1');
        phi_1(j,ir) = f.b1;

    end
end

% impurity B
data{1,1}='gate (V)';
data{1,2}='filling_factor';
for i = 1:length(rx(1,:))
    data{1,i+2}=rx(1,i);
end
for i = 1:length(ind)
    data{i+1,1} = gate_range(i);
    data{i+1,2} = n(i);
    for j = 1:length(rx(1,:))
        data{i+1,j+2}=phi_1(i,j);
    end
end

writecell(data,'multi_filling_lineB.csv')

% impurity C
% data{1,1}='gate (V)';
% data{1,2}='filling_factor';
% for i = 1:126
%     data{1,i+2}=rx(1,i);
% end
% for i = 1:length(ind)
%     data{i+1,1} = gate_range(i);
%     data{i+1,2} = n(i);
%     for j = 126:-1:1
%         data{i+1,j+2}=phi_1(i,j);
%     end
% end

% dataz{1,1}='gate (V)';
% dataz{1,2}='filling_factor';
% for i = 1:116
%     dataz{1,i+2}=rx(1,i);
% end
% for i = 1:length(ind)
%     dataz{i+1,1} = gate_range(i);
%     dataz{i+1,2} = n(i);
%     for j = 126:241
%         dataz{i+1,j+2}=zzz(i,j);
%     end
% end

% writecell(data,'multi_filling_lineC.csv')
% writecell(dataz,'multi_filling_lineC2.csv')

end
